#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //创建行
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);

    //创建列
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    //设置列表头（水平）
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("成绩"));

    //设置行表头（垂直）
    ui->tableWidget->setVerticalHeaderItem(0,new QTableWidgetItem("1"));
    ui->tableWidget->setVerticalHeaderItem(1,new QTableWidgetItem("2"));
    ui->tableWidget->setVerticalHeaderItem(2,new QTableWidgetItem("3"));

    //添加初始化数据
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("10"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("0"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("20"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("100"));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_row_clicked()
{
    //获取行表头
    QString str(ui->lineEdit->text());
    //获取当前的行数
    int row=ui->tableWidget->rowCount();
    //添加一行
    ui->tableWidget->insertRow(row);
    //设置行表头
    ui->tableWidget->setVerticalHeaderItem(row,new QTableWidgetItem(str));


}

void Widget::on_pushButton_delete_row_clicked()
{
    //获取当前选中的行
    int row=ui->tableWidget->currentRow();
    //进行删除
    ui->tableWidget->removeRow(row);
}

void Widget::on_pushButton_add_column_clicked()
{
    //获取列表头
    QString str(ui->lineEdit->text());
    //获取当前的列数
    int column=ui->tableWidget->columnCount();
    //添加一行
    ui->tableWidget->insertColumn(column);
    //设置行表头
    ui->tableWidget->setHorizontalHeaderItem(column,new QTableWidgetItem(str));
}

void Widget::on_pushButton_delete_column_clicked()
{
    //获取当前选中的列
    int column=ui->tableWidget->currentColumn();
    //进行删除
    ui->tableWidget->removeColumn(column);
}
